<?php
namespace frontend\Components;
use yii\web\Application;
use yii\base\Behavior;
use common\models\User;

class AuthBehavior extends Behavior
{

    public function events()
    {
        return [
            Application::EVENT_BEFORE_ACTION => 'beforeAction',
        ];
    }

    public function beforeAction($event)
    {
        $session = \Yii::$app->session;
        if(!$session->has('access_token')){
            $session['route_url'] = \Yii::$app->requestedRoute;
            $redirect_url = 'http://tp.lsol.com.cn/auth';
            $url = \Yii::$app->wechat->getOauthRedirect($redirect_url);
            \Yii::$app->getResponse()->redirect($url)->send();
            exit;
        }

        //设置全局属性
        $userinfo = \Yii::$app->wechat->getOauthUserinfo($session['access_token']['access_token'],$session['access_token']['openid']);


        \Yii::$app->params['userinfo'] = $userinfo;



    }

}
